package cn.sciento.farm.devicemanager.service.impl;

import cn.sciento.farm.devicemanager.dao.entity.DevScreen;
import cn.sciento.farm.devicemanager.dao.entity.DevScreenDeviceRel;
import cn.sciento.farm.devicemanager.dao.mapper.DevScreenMapper;
import cn.sciento.farm.devicemanager.dao.model.ScreenForm;
import cn.sciento.farm.devicemanager.service.ScreenDeviceRelService;
import cn.sciento.farm.devicemanager.service.ScreenService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScreenServiceImpl implements ScreenService {

    @Autowired
    private DevScreenMapper screenMapper;

    @Autowired
    private ScreenDeviceRelService screenDeviceRelService;

    @Override
    public int create(ScreenForm screen) {
        int count = screenMapper.insert(screen);
        if(count >0){
            screenDeviceRelService.deleteByScreenId(screen.getId());
            addRel(screen.getId(),screen.getDeviceIds());
        }
        return count;
    }

    private int addRel(long screenId,List<String> deviceIds){
        int count = 0;
        for(String deviceId:deviceIds){
            DevScreenDeviceRel rel = new DevScreenDeviceRel();
            rel.setDeviceId(deviceId);
            rel.setScreenId(screenId);
            count += screenDeviceRelService.create(rel);
        }
        return count;
    }



    @Override
    public int updateById(ScreenForm screen) {
        int count = screenMapper.updateById(screen);
        if(count >0){
            screenDeviceRelService.deleteByScreenId(screen.getId());
            addRel(screen.getId(),screen.getDeviceIds());
        }
        return count;
    }

    @Override
    public int deleteById(Long id) {
        int count = screenMapper.deleteById(id);
        screenDeviceRelService.deleteByScreenId(id);
        return count;
    }

    @Override
    public List<ScreenForm> getByAreaId(Long areaId) {
        List<ScreenForm> screenList = screenMapper.getByAreaId(areaId);
        return screenList;
    }

    @Override
    public ScreenForm getById(Long id) {
        DevScreen devScreen  = screenMapper.selectById(id);
        if(devScreen == null){
            return null;
        }
        List<String> deviceIds = screenDeviceRelService.getDeviceIds(id);
        ScreenForm screenForm = new ScreenForm();
        BeanUtils.copyProperties(devScreen,screenForm);
        screenForm.setDeviceIds(deviceIds);
        return screenForm;
    }

    @Override
    public int deleteByAreaId(Integer areaId) {
        QueryWrapper queryWrapper = new QueryWrapper<DevScreen>();
        queryWrapper.eq("area_id",areaId);
        int count = screenMapper.delete(queryWrapper);
        return count;
    }

    @Override
    public ScreenForm getByEui(String eui) {
        QueryWrapper queryWrapper = new QueryWrapper<DevScreen>();
        queryWrapper.eq("eui",eui);
        DevScreen devScreen  = screenMapper.selectOne(queryWrapper);
        if(devScreen == null){
            return null;
        }
        List<String> deviceIds = screenDeviceRelService.getDeviceIds(devScreen.getId());
        ScreenForm screenForm = new ScreenForm();
        BeanUtils.copyProperties(devScreen,screenForm);
        screenForm.setDeviceIds(deviceIds);
        return screenForm;
    }
}
